package com.news.model;

/**
 * 文章图片数据模型
 */
public class ArticleImage {
    private String url;           // 图片URL
    private String alt;           // 图片描述文字
    private String title;         // 图片标题
    private String caption;       // 图片说明
    private String localPath;     // 本地缓存路径
    private int width;            // 图片宽度
    private int height;           // 图片高度
    private long fileSize;        // 文件大小（字节）
    private boolean isLoaded;     // 是否已加载
    private boolean isMainImage;  // 是否为主图

    public ArticleImage() {
        this.isLoaded = false;
        this.isMainImage = false;
    }

    public ArticleImage(String url) {
        this();
        this.url = url;
    }

    public ArticleImage(String url, String alt) {
        this(url);
        this.alt = alt;
    }

    // Getters and Setters
    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getAlt() {
        return alt;
    }

    public void setAlt(String alt) {
        this.alt = alt;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCaption() {
        return caption;
    }

    public void setCaption(String caption) {
        this.caption = caption;
    }

    public String getLocalPath() {
        return localPath;
    }

    public void setLocalPath(String localPath) {
        this.localPath = localPath;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public long getFileSize() {
        return fileSize;
    }

    public void setFileSize(long fileSize) {
        this.fileSize = fileSize;
    }

    public boolean isLoaded() {
        return isLoaded;
    }

    public void setLoaded(boolean loaded) {
        isLoaded = loaded;
    }

    public boolean isMainImage() {
        return isMainImage;
    }

    public void setMainImage(boolean mainImage) {
        isMainImage = mainImage;
    }

    /**
     * 获取显示名称
     */
    public String getDisplayName() {
        if (title != null && !title.trim().isEmpty()) {
            return title;
        }
        if (alt != null && !alt.trim().isEmpty()) {
            return alt;
        }
        if (caption != null && !caption.trim().isEmpty()) {
            return caption;
        }
        return "图片";
    }

    /**
     * 获取文件扩展名
     */
    public String getFileExtension() {
        if (url == null) return "";
        
        int lastDot = url.lastIndexOf('.');
        int lastSlash = url.lastIndexOf('/');
        int queryStart = url.indexOf('?', lastDot);
        
        if (lastDot > lastSlash && lastDot < url.length() - 1) {
            int endIndex = queryStart > 0 ? queryStart : url.length();
            return url.substring(lastDot + 1, endIndex).toLowerCase();
        }
        return "";
    }

    /**
     * 检查是否为有效的图片URL
     */
    public boolean isValid() {
        return url != null && !url.trim().isEmpty() && 
               (url.startsWith("http://") || url.startsWith("https://"));
    }

    /**
     * 获取图片尺寸信息
     */
    public String getDimensionInfo() {
        if (width > 0 && height > 0) {
            return width + "×" + height;
        }
        return "";
    }

    /**
     * 获取文件大小信息
     */
    public String getFileSizeInfo() {
        if (fileSize <= 0) return "";
        
        if (fileSize < 1024) {
            return fileSize + " B";
        } else if (fileSize < 1024 * 1024) {
            return String.format("%.1f KB", fileSize / 1024.0);
        } else {
            return String.format("%.1f MB", fileSize / (1024.0 * 1024.0));
        }
    }

    @Override
    public String toString() {
        return "ArticleImage{" +
                "url='" + url + '\'' +
                ", alt='" + alt + '\'' +
                ", isLoaded=" + isLoaded +
                ", isMainImage=" + isMainImage +
                '}';
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        
        ArticleImage that = (ArticleImage) obj;
        return url != null ? url.equals(that.url) : that.url == null;
    }

    @Override
    public int hashCode() {
        return url != null ? url.hashCode() : 0;
    }
} 